﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Model;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;

namespace SMTC.Dao.BaseInfo
{
    public class ProductBaseInfoDao : BaseDao
    {
        /// <summary>
        /// 插入产品
        /// </summary>
        private const string SQL_INSERT_PRODUCTBASE = @"INSERT INTO dbo.SMT_ProductBaseInfo (
	                                                        ID,
	                                                        ProductCode,
                                                            Para,Operation,OperationTime,UnitCount,isTemp
                                                        ) VALUES ( 
	                                                        /* ID - uniqueidentifier */ NEWID(),
	                                                        /* ProductCode - nvarchar(50) */ @ProductCode,                                                       
                                                                                  @Para,@Operation,@OperationTime,@UnitCount,@isTemp
                                                                           ) ";
        /// <summary>
        /// 更新产品信息
        /// </summary>
        private const string SQL_UPDATE_PRODUCTBASE = @"UPDATE dbo.SMT_ProductBaseInfo
    SET Para = @Para,Operation = @Operation,OperationTime = @OperationTime,UnitCount = @UnitCount,isTemp = @isTemp
    WHERE ProductCode = @ProductCode";

        /// <summary>
        /// 插入程序名
        /// </summary>
        private const string SQL_INSERT_Program = @"INSERT INTO dbo.SMT_HalfToProgram (
	                                                ID,
	                                                HalfProductCode,
	                                                ProgramName,
	                                                UnitCount,Operation,OperationTime
                                                ) VALUES ( 
	                                                /* ID - uniqueidentifier */ NEWID(),
	                                                /* HalfProductCode - nvarchar(50) */ @HalfProductCode,
	                                                /* ProgramName - nvarchar(50) */ @ProgramName,
	                                                /* UnitCount - int */ @UnitCount,@Operation,@OperationTime ) ";
        /// <summary>
        /// 更新程序名
        /// </summary>
        private const string SQL_UPDATE_PROGRAM = @"UPDATE dbo.SMT_HalfToProgram
                                                    SET UnitCount = @UnitCount
                                                       ,ProgramName = @ProgramName
                                                        ,Operation = @Operation
                                                        ,OperationTime = @OperationTime
                                                    WHERE HalfProductCode = @HalfProductCode";
        //        private const string SQL_UPDATE_Program = @"UPDATE dbo.SMT_ProductBaseInfo
        //                                                        SET ProgramName=@ProgramName
        //                                                        WHERE  HalfProductCode = @HalfProductCode";
        /// <summary>
        /// 删除产品信息
        /// </summary>
        private const string SQL_DELETE_PRODECTBASE = @"DELETE dbo.SMT_ProductBaseInfo WHERE ID = @ID";
        /// <summary>
        /// 获取产品信息列表
        /// </summary>
        private const string SQL_SELECT_PRODUCT_LIST = @"SELECT ID,
                                                               ProductCode,
                                                               HalfProductCode,
                                                               UnitCount,Para,ProgramName,
                                                               Operation,OperationTime,ISNULL(isTemp,0) isTemp
                                                               FROM dbo.SMT_ProductBaseInfo
                                                        WHERE ProductCode LIKE @ProductCode";
        /// <summary>
        /// 获取产品信息实体
        /// </summary>
        private const string SQL_SLELECT_PRODUCT = @"SELECT ID,
	                                                       ProductCode,
	                                                       HalfProductCode,
	                                                       UnitCount,ProgramName FROM dbo.SMT_ProductBaseInfo
                                                    WHERE ID = @ID";

        /// <summary>
        /// 获取产品信息实体
        /// </summary>
        private const string SQL_SLELECT_PRODUCT_Pro = @"SELECT ID,
	                                                       ProductCode,
	                                                       HalfProductCode,
	                                                       UnitCount,ProgramName FROM dbo.SMT_ProductBaseInfo
                                                    WHERE  ProductCode = @ProductCode";

        /// <summary>
        /// 获取半成品程序名信息实体
        /// </summary>
        private const string SQL_SLELECT_halfProduct = @"SELECT TOP 1	ID,
	                                                    HalfProductCode,
	                                                    ProgramName,
	                                                    UnitCount
	                                                    FROM dbo.SMT_HalfToProgram
	                                                    WHERE HalfProductCode=@HalfProductCode 
	                                                    ";

        private const string SQL_SELECT_PRODUCTINFO_PRO = @"SELECT * FROM SMT_ProductBaseInfo
                                                            WHERE ProductCode = @ProductCode";

        /// <summary>
        /// 插入产品信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertProductBase(SMT_ProductBaseInfoModel model)
        {

            DbParameter[] parameters = 
                {
                    DBparameterBuild.Build("@ProductCode",model.ProductCode)
                   ,DBparameterBuild.Build("@Para",model.Para)
                   ,DBparameterBuild.Build("@Operation",model.Operation)
                   ,DBparameterBuild.Build("@OperationTime",model.OperationTime)
                   ,DBparameterBuild.Build("@UnitCount",model.UnitCount)
                   ,DBparameterBuild.Build("@isTemp",model.isTemp)
                };
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_PRODUCTBASE, parameters) > 0;
        }

        /// <summary>
        /// 插入产品信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateProductBase(SMT_ProductBaseInfoModel model)
        {

            DbParameter[] parameters = 
                                        {                                           
                                           DBparameterBuild.Build("@Para",model.Para)
                                           ,DBparameterBuild.Build("@ProductCode",model.ProductCode)
                                           ,DBparameterBuild.Build("@Operation",model.Operation)
                                           ,DBparameterBuild.Build("@OperationTime",model.OperationTime)
                                           ,DBparameterBuild.Build("@UnitCount",model.UnitCount)
                                           ,DBparameterBuild.Build("@isTemp",model.isTemp)
                                        };
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_PRODUCTBASE, parameters) > 0;


        }

        public bool InsertHalfProductBase(SMT_ProductBaseInfoModel model)
        {
            //将程序名插入到新表中
            DbParameter[] parameters = 
                                        {
                                           DBparameterBuild.Build("@HalfProductCode",model.HalfProductCode)
                                           ,DBparameterBuild.Build("@ProgramName",model.ProgramName)
                                           ,DBparameterBuild.Build("@UnitCount",model.UnitCount)
                                           ,DBparameterBuild.Build("@Operation",model.Operation)
                                           ,DBparameterBuild.Build("@OperationTime",model.OperationTime)
                                        };
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_Program, parameters) > 0;
        }

        public bool UpdateHalfProductBase(SMT_ProductBaseInfoModel model)
        {
            DbParameter[] parameters = 
                                        {
                                           DBparameterBuild.Build("@UnitCount",model.UnitCount)
                                           ,DBparameterBuild.Build("@ProgramName",model.ProgramName)
                                           ,DBparameterBuild.Build("@HalfProductCode",model.HalfProductCode)
                                           ,DBparameterBuild.Build("@Operation",model.Operation)
                                           ,DBparameterBuild.Build("@OperationTime",model.OperationTime)
                                        };
            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_PROGRAM, parameters) > 0;
        }


        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool DeleteProductBase(string ID)
        {
            DbParameter[] parameters = 
            {
               DBparameterBuild.Build("@ID",ID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_PRODECTBASE, parameters) > 0;
        }
        /// <summary>
        /// 获取产品信息列表
        /// </summary>
        /// <param name="ProductCode"></param>
        /// <param name="HalfProductCode"></param>
        /// <returns></returns>
        public List<SMT_ProductBaseInfoModel> GetProductBaseList(string ProductCode)
        {
            DbParameter[] parameters = 
            {
               DBparameterBuild.Build("@ProductCode",ProductCode)
            };

            return DBTemplete.ExcuteGetList<SMT_ProductBaseInfoModel>(CommandType.Text, SQL_SELECT_PRODUCT_LIST, parameters);
        }
        /// <summary>
        /// 获取产品信息实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public SMT_ProductBaseInfoModel GetProductBase(string ID)
        {
            DbParameter[] parameters = 
            {
               DBparameterBuild.Build("@ID",ID)
            };

            return DBTemplete.ExcuteGetObject<SMT_ProductBaseInfoModel>(CommandType.Text, SQL_SLELECT_PRODUCT, parameters);
        }
        /// <summary>
        /// 获取产品信息实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public SMT_ProductBaseInfoModel GetProductBaseByProduct(string Product)
        {
            if (string.IsNullOrEmpty(Product)) Product = "";

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@ProductCode",Product)
            };

            return DBTemplete.ExcuteGetObject<SMT_ProductBaseInfoModel>(CommandType.Text, SQL_SLELECT_PRODUCT_Pro, parameters);
        }
        /// <summary>
        /// 获取半成品程序名信息实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public SMT_ProductBaseInfoModel GethalfProductBaseByProduct(string halfProduct)
        {
            if (string.IsNullOrEmpty(halfProduct)) halfProduct = "";

            DbParameter[] parameters = 
            {
               DBparameterBuild.Build("@HalfProductCode",halfProduct)
            };

            return DBTemplete.ExcuteGetObject<SMT_ProductBaseInfoModel>(CommandType.Text, SQL_SLELECT_halfProduct, parameters);
        }

        public List<SMT_ProductBaseInfoModel> getProduct(string Product)
        {
            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@ProductCode",Product)
            };

            return DBTemplete.ExcuteGetList<SMT_ProductBaseInfoModel>(CommandType.Text, SQL_SELECT_PRODUCTINFO_PRO, parameters);
        }

    }
}
